package com.sixkey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.sixkey.constant.CommonConstant;
import com.sixkey.entity.Notice;
import com.sixkey.entity.User;
import com.sixkey.entity.vo.NoticeVo;
import com.sixkey.exception.ServiceException;
import com.sixkey.mapper.NoticeMapper;
import com.sixkey.response.ResponseEnum;
import com.sixkey.service.INoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-18
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    /**
     * 批量或单个公告删除
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeWithType(List<Integer> id) {
        List<Notice> notices = baseMapper.selectBatchIds(id);
        //过滤掉未发布公告剩下就是发布中的公告
        List<Notice> list = notices.stream().filter(notice -> notice.getStatus() == 0).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(list)){
            //说明有发布中的公告
            throw new ServiceException(ResponseEnum.NOTICE_PUBLISHED);
        }
        //如果可以删除，删除公告表中的数据
        this.removeByIds(id);
    }

    /**
     * 获取公告分页数据
     * @param pageNo
     * @param pageSize
     * @param title
     * @return
     */
    @Override
    public Page<Notice> noticePage(int pageNo, int pageSize, String title) {
        //构造分页构造器
        Page<Notice> pageInfo = new Page<>(pageNo,pageSize);
        //构造查询构造器
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        //根据公告名称进行模糊查询
        queryWrapper.like(!StringUtils.isEmpty(title),Notice::getTitle,title);
        //添加排序条件，根据修改时间排序
        queryWrapper.orderByDesc(Notice::getUpdateTime);
        Page<Notice> noticePage = this.page(pageInfo, queryWrapper);
        return noticePage;
    }

    /**
     * 修改公告
     * @param notice
     */
    @Override
    public void updateNotice(Notice notice) {
        Notice updateNotice = this.getById(notice.getId());
        if(updateNotice.getStatus() == CommonConstant.NOTICE_STATUS_YES){
            //如果公告是处于发布状态，则需先撤销才能修改
            throw new ServiceException(ResponseEnum.MEET_PASSED_CHEXIAO);
        }
        //修改
        this.updateById(notice);
    }

    /**
     * 修改公告状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        Notice notice = this.getById(id);
        notice.setStatus(status);
        this.updateById(notice);
    }

    /**
     * 批量发布公告
     * @param id
     */
    @Override
    public void publishBatch(List<Integer> id) {
        List<Notice> notices = this.listByIds(id);
        //将公告都设置为发布状态
        notices.stream().forEach(notice -> {
            notice.setStatus(0);
        });
        //修改
        this.updateBatchById(notices);
    }

    /**
     * 批量撤销公告
     * @param id
     */
    @Override
    public void unDoStatus(List<Integer> id) {
        List<Notice> notices = this.listByIds(id);
        //将公告都设置为未发布状态
        notices.stream().forEach(notice -> {
            notice.setStatus(1);
        });
        //修改
        this.updateBatchById(notices);
    }

    /**
     * 小程序端首页获取公告内容
     * @return
     */
    @Override
    public List<String> getNotice() {
        //查询已经发布的公告
        List<Notice> noticeList = this.list(new QueryWrapper<Notice>().eq("status", 0));
        List<String> notices = new ArrayList<>();
        noticeList.stream().forEach(notice -> {
            String noticeStr = new String();
            noticeStr = notice.getContent();
            notices.add(noticeStr);
        });
        return notices;
    }
}
